{layout name="common/layout" /}
<section class="panel">
    <header class="panel-heading">
        客服权限组添加
    </header>
    <div class="panel-body">
        <div class="alert alert-warning text-center">权限组最高添加16个(可根据部门划分)</div>
        <form class="form-horizontal" id="group_form" >
            <div class="form-group">
                <label  class="col-lg-2 col-sm-2 control-label">权限组名称</label>
                <div class="col-lg-10">
                    <input type="text" name="title" class="form-control" value="" >
                    <p class="help-block">如:拼图云商城客服组</p>
                </div>
            </div>
            {volist name="rules" id="rule"}
            <div class="form-group">
                <div class="col-lg-10 col-lg-offset-2">
                    <div class="row"><div class="col-xs-12"><input type="checkbox" name="rule[]" value="{$rule['id']}" id="group{$rule['id']}" > {$rule['title']}</div></div>
                    {php} if(!empty($rule['child'])){ {/php}

                    <div class="row">
                        {volist name="rule['child']" id="child"}
                        <div class="col-xs-2"><input type="checkbox" name="rule[]" value="{$child['id']}" data-group="group{$rule['id']}"> {$child['title']}</div>
                        {/volist}
                    </div>
                    {php} } {/php}
                </div>
            </div>
            {/volist}
            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </div>
        </form>
    </div>
</section>
<script type="text/javascript">
    $(document).ready(function () {
        {volist name="rules" id="rule"}
        $('#group{$rule['id']}').unbind('click').bind('click',function () {
            if(this.checked){
                $('[data-group="group{$rule['id']}"]').prop("checked", true);
            }else{
                $('[data-group="group{$rule['id']}"]').prop("checked", false);
            }
        });
        {/volist}
        $('input[data-group]').unbind('click').bind('click',function () {
            if(this.checked){
                $('#'+$(this).data('group')).prop("checked", true);
            }else{
                if($('input[data-group]:checked').length==0){
                    $('#'+$(this).data('group')).prop("checked", false);
                }
            }
        });
    });
    $('#group_form').submit(function () {
        var title = $('input[name="title"]').val();
        if(!title){
            toastTop('权限组名称不能为空',2);
            return false;
        }
        $.ajax({
                cache: true,
                type: "POST",
                url:'{:WebUrl('admincustom/addgroup')}',
                data:$('#group_form').serialize(),
                async: false,
                error: function(data) {
                    toastTop('服务器错误，稍后再试',2);
                },
                success: function(data) {
                     if(data.status=1){
                         toastTop('添加权限组成功');
                         setTimeout(function(){
                            window.location='{:WebUrl('admincustom/grouplist')}';
                         },600)
                     }else{
                         toastTop(data.info);
                     }
                 }
        },'json');
        return false;
    });
</script>